package com.carkepper.appraisal.service.impl;

import com.carkeeper.entity.PageResult;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkepper.appraisal.dao.AppraisalMongoDao;
import com.carkepper.appraisal.service.AppraisalMongoService;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 服务评价的mongo的service的实现类
 */
@Service
public class AppraisalMongoServiceImpl implements AppraisalMongoService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     *  1.增加评论
     *
     * @param
     * @return
     */
    public Result<AppraisalMongoDao> saveObj(AppraisalMongoDao mongoDao, String fileFull) {
        // 赋值图片的回显路径
        mongoDao.setImg(fileFull);
//        mongoDao.setImg("我是一张测试图片^_^");
        // 增加评论信息
        AppraisalMongoDao save = mongoTemplate.save(mongoDao);
        if (save != null){
            return new Result<AppraisalMongoDao>(true, StatusCode.OK,"增加成功");
        }
        return new Result<AppraisalMongoDao>(true, StatusCode.OK,"增加失败");
    }

    /**
     *  2.查询所有
     * @return
     */
    public Result<AppraisalMongoDao> findAll() {
        List<AppraisalMongoDao> list = mongoTemplate.findAll(AppraisalMongoDao.class);
        if (list != null){
            return new Result<AppraisalMongoDao>(true, StatusCode.OK,"查询全部成功",list);
        }
        return new Result<AppraisalMongoDao>(true, StatusCode.OK,"门店不存在或者输入错误");
    }

    /***
     *  3.根据id查询
     * @param id
     * @return
     */
    public Result<AppraisalMongoDao> getAppraiseById(Integer id) {
        //创建查询条件
        Query query = new Query(Criteria.where("_id").is(id));
        AppraisalMongoDao one = mongoTemplate.findOne(query, AppraisalMongoDao.class);
        if (one != null){
            return new Result<AppraisalMongoDao>(true, StatusCode.OK,"查询成功",one);
        }
        return new Result<AppraisalMongoDao>(true, StatusCode.OK,"查询失败");
    }


    /***
     * 删除对象
     * @param
     * @return
     */
    public Result<AppraisalMongoDao> deleteAppraisalMongoDao(AppraisalMongoDao mongoDao) {
        DeleteResult remove = mongoTemplate.remove(mongoDao);
        if (remove != null){
            return new Result<AppraisalMongoDao>(true, StatusCode.OK,"删除成功!");
        }
        return new Result<AppraisalMongoDao>(true, StatusCode.OK,"删除失败!");
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    public Result<AppraisalMongoDao> deleteAppraiseBy(Integer id) {
        // 先根据id查询 findOne
        Result<AppraisalMongoDao> appraiseById = getAppraiseById(id);
        // 再删除该对象 delete
        Result<AppraisalMongoDao> result = deleteAppraisalMongoDao(appraiseById.getData());
        if (20000 == result.getCode()){
            return new Result<AppraisalMongoDao>(true, StatusCode.OK,"删除成功!");
        }
        return new Result<AppraisalMongoDao>(true, StatusCode.OK,"删除失败!");
    }

    /**
     * 分页查询，点击服务评价就调用这个服务
     *
     * @param pageIndex 当前页
     * @param pageSize  每页条数
     * @return
     */
    public PageResult<AppraisalMongoDao> pageQuery(int pageIndex, int pageSize) {
        // 设置起始数
        Query query = new Query();
        query.skip((pageIndex - 1) * pageSize)
                // 设置查询条数
                .limit(pageSize);
        // 查询记录总数
        Long totalCount = mongoTemplate.count(query, AppraisalMongoDao.class);
        // 数据总页数
        Long totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        // 查询总数据
        List<AppraisalMongoDao> list = mongoTemplate.find(query, AppraisalMongoDao.class);
        // 计算评价得分后的数据
        /*List<AppraisalMongoDao> listFinally = setScoreFinally(list);*/
        // 返回给前端
        PageResult<AppraisalMongoDao> pageResult = new PageResult<>();
        pageResult.setTotal(totalCount);
        pageResult.setRows(list);
        return pageResult;
    }

    /**
     * 多条件查询加分页
     *
     * @param orderId         预约单号
     * @param customorName    客户名称
     * @param orderCreateTime 订单时间
     * @param storeSimpleName 客户门店
     * @return
     */
    public PageResult<AppraisalMongoDao> getAppraisalMongoDaoByQuery(String orderId, String customorName,
                                                                     String orderCreateTime, String storeSimpleName,
                                                                     String serviceKind,
                                                                     Integer pageIndex, Integer pageSize) {
        // 设置起始数
        Query query = new Query();
        query.skip((pageIndex - 1) * pageSize)
                // 设置查询条数
                .limit(pageSize);
        // 查询记录总数
        Long totalCount = mongoTemplate.count(query, AppraisalMongoDao.class);
        // 判断条件是否为空
        if (orderId != null && "" != orderId) {
            query.addCriteria(Criteria.where("orderId").is(orderId));
        }
        if (customorName != null && "" != customorName) {
            query.addCriteria(Criteria.where("customorName").is(customorName));
        }
        if (orderCreateTime != null && "" != orderCreateTime) {
            query.addCriteria(Criteria.where("orderCreateTime").is(orderCreateTime));
        }
        if (storeSimpleName != null && "" != storeSimpleName) {
            query.addCriteria(Criteria.where("storeSimpleName").is(storeSimpleName));
        }
        if (serviceKind != null && "" != serviceKind) {
            query.addCriteria(Criteria.where("serviceKind").is(serviceKind));
        }
        // 查询结果
        List<AppraisalMongoDao> list = mongoTemplate.find(query, AppraisalMongoDao.class);
        // 计算评价得分后的数据
        /*List<AppraisalMongoDao> listFinally = setScoreFinally(list);*/
        // 返回给前端
        PageResult<AppraisalMongoDao> pageResult = new PageResult<>();
        pageResult.setTotal(totalCount);
        pageResult.setRows(list);
        return pageResult;
    }

    /**
     * 逻辑删除的方法
     *
     * @return
     */
    public Result<AppraisalMongoDao> update(Integer Id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(Id));
        Update update = Update.update("delete", 1);
        //先查询一下，该数据的delete字段
        Result<AppraisalMongoDao> appraiseById = getAppraiseById(Id);
        if (appraiseById.getData().getDelete() == 1){
            return new Result<AppraisalMongoDao>(true, StatusCode.ERROR,"该评论已经被删除了!");
        }
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, AppraisalMongoDao.class);
        if (updateResult != null){
            return new Result<AppraisalMongoDao>(true, StatusCode.OK,"删除成功!");
        }
        return new Result<AppraisalMongoDao>(true, StatusCode.ERROR,"删除失败!");
    }

    /**
     * 逻辑删除恢复的方法
     *
     * @return
     */
    public Result<AppraisalMongoDao> updateBack(Integer Id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(Id));
        Update update = Update.update("delete", 0);
        //先查询一下，该数据的delete字段
        Result<AppraisalMongoDao> appraiseById = getAppraiseById(Id);
        if (appraiseById.getData().getDelete() == 0){
            return new Result<AppraisalMongoDao>(true, StatusCode.ERROR,"该评论已经存在!");
        }
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, AppraisalMongoDao.class);
        if (updateResult != null){
            return new Result<AppraisalMongoDao>(true, StatusCode.OK,"删除恢复!");
        }
        return new Result<AppraisalMongoDao>(true, StatusCode.ERROR,"删除失败!");
    }


    /**
     * 按照店铺计算评价总分
     * 质量评分40%
     * 物流评分30%
     * 商家态度评分30%
     * @param list
     * @return
     */
    /*public List<AppraisalMongoDao> setScoreFinally(List<AppraisalMongoDao> list){
        for (AppraisalMongoDao appraisalMongoDao : list) {
            String scoreQulity = appraisalMongoDao.getScoreQulity();//质量评分40%
            int scoreQulityInt = Integer.valueOf(scoreQulity);
            scoreQulityInt = (int) (scoreQulityInt * 0.4);
            String scoreTransform = appraisalMongoDao.getScoreTransform();//商家态度评分30%
            int scoreTransformInt = Integer.valueOf(scoreTransform);
            scoreTransformInt = (int) (scoreTransformInt * 0.3);
            String scoreStore = appraisalMongoDao.getScoreStore();//物流评分30%
            int scoreStoreInt = Integer.valueOf(scoreStore);
            scoreStoreInt = (int) (scoreStoreInt * 0.4);
            appraisalMongoDao.setScoreFinally(String.valueOf(scoreQulityInt + scoreTransformInt + scoreStoreInt));
        }
        return list;
    }*/

}
